package com.itheima.controller;

import com.itheima.entity.User;
import com.itheima.service.UserService;
import com.itheima.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/login")
    public R<Object> login(@RequestBody User user, HttpServletRequest request){
              log.info(user.getPhone());

        User resultUser = userService.selectOne(user);
        if (resultUser == null ){
            //新用户, 自动注册
            user.setStatus(1); //状态
           userService.addUser(user);

        }
          //老用户

        request.getSession().setAttribute("user",resultUser.getId());
        return R.success(user);
    }

}
